package com.aftersale.demo.service.impl;

import com.aftersale.demo.entity.Result;
import com.aftersale.demo.utils.MyUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aftersale.demo.entity.Notice;
import com.aftersale.demo.service.NoticeService;
import com.aftersale.demo.mapper.NoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 *
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
    implements NoticeService{

    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public Result getNotice(Integer pageNum, Integer pageSize) {
        Page<Notice> noticePage=page(new Page<>(pageNum,pageSize));
        return Result.success(noticePage);
    }

    @Override
    public Result selectNotice(Integer pageNum, Integer pageSize, String title) {
        QueryWrapper<Notice> queryWrapper=new QueryWrapper();
        queryWrapper.like("title",title);
        Page<Notice> noticePage=page(new Page<>(pageNum,pageSize),queryWrapper);
        return Result.success(noticePage);
    }

    @Override
    public Result addNotice(Notice notice) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now=sdf.format(new Date());
        notice.setTime(now);
        Boolean key=save(notice);
        if(key){
            return Result.success("添加成功");
        }else {
            return Result.error("401","添加失败");
        }
    }

    @Override
    public Result deleteNotice(int id) {
        if(Objects.isNull(getById(id))){
            return Result.error("401","公告不存在");
        }
        noticeMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @Override
    public Result updateNotice(Notice notice) {
        if(Objects.isNull(getById(notice.getId()))){
            return Result.error("401","公告不存在");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now=sdf.format(new Date());
        notice.setTime(now);
        Boolean key=updateById(notice);
        if(key){
            return Result.success("修改成功");
        }else{
            return Result.error("401","修改失败");
        }
    }

    @Override
    public Result getOpenNotice(Integer pageNum, Integer pageSize) {
        QueryWrapper<Notice> noticeQueryWrapper=new QueryWrapper<>();
        noticeQueryWrapper.like("open",1);
        Page<Notice> noticePage=page(new Page<>(pageNum,pageSize),noticeQueryWrapper);
        return Result.success(noticePage);
    }
}




